
#include "AngleAlgorithm.h"
#include <math.h>


namespace {
	int isSinMix = 1;
}
AngleAlgorithm::AngleAlgorithm()
	: _angle(0)
	, _size(0)
{
	_lastPos.pos = 0;
	_lastPos.valve = 1;
	_lastBegin.pos = 0;
	_lastBegin.valve = 1;
	_lastEnd.pos = 1;
	_lastEnd.valve = 0;

}

AngleAlgorithm::~AngleAlgorithm()
{
}

void AngleAlgorithm::setSize(int size) {
	_size = size;
	//setAngle(70);
}

int AngleAlgorithm::changeMix() {
	if (isSinMix)
	{
		isSinMix = 0;
	}else{
		isSinMix = 1;
	}
	return 1;
}

void AngleAlgorithm::setAngle(float angle) {
	if (_size < 2) {
		return ;
	}

	while (angle >= 360){	angle -= 360;}
	while (angle < 0){	angle += 360;}
	_angle = angle;
	//计算音频流A的值
	float newPos = angle * _size / 360.0f;
    
    //float非精准计算，有可能会越界
    if ((int)newPos == _size) {
        newPos = 0;
    }
    
	if (isSinMix){
		POS_VALVE outBegin;
		POS_VALVE outEnd;
		//计算出曲线的两端
		getInterval(_lastBegin, _lastEnd, _lastPos, newPos, &outBegin, &outEnd);
		_lastBegin = outBegin;
		_lastEnd = outEnd;
		//计算出比例
		float valve = calcPosValve(outBegin, outEnd, newPos);
		//LOGE("pos:%f, valve:%f", newPos, valve);
		_lastPos.pos = newPos;
		_lastPos.valve = valve;
	}else{
		_lastPos.pos = newPos;
		_lastPos.valve = 1 - (newPos - (int)newPos);
	}
	//测试用的，用于绘制mix曲线
	//ChartOpenGL::getInstance()->addPOS_VALVE(_lastPos);
}

bool AngleAlgorithm::getProportion(int* idA, int* idB, float* proportionA, float* proportionB) {
	if (_size  == 1)
	{
		*idA = 0;
		*idB = 0;
		*proportionA = 1.0f;
		*proportionB = 0.0f;
		return true;
	}else if(_size <1){
		return false;
	}

	*idA = _lastPos.pos;
	*idB = *idA + 1;
	if (*idB == _size)	*idB = 0;
	*proportionA= _lastPos.valve;
	*proportionB = 1.0f-_lastPos.valve;
	
	return true;
}


//判断是否是同一条曲线
bool AngleAlgorithm::isSameLine(float beginPos, float endPos, float lastPos, float newPos) {
	bool ret = false;
	if (endPos > lastPos ) {//right
		if (newPos >= lastPos && newPos <= endPos)
		{
			ret = true;
		}
	}else{		//left
		if (newPos <= lastPos && newPos >= endPos)
		{
			ret = true;
		}
	}
	return ret;
}


//计算一条曲线中某个点的值
float AngleAlgorithm::calcPosValve(POS_VALVE lastBegin, POS_VALVE lastEnd, float pos) {
	float ret = 0;
	if (lastEnd.pos > lastBegin.pos)  //right
	{
		float radian = M_PI_2 * (pos - lastBegin.pos) / (lastEnd.pos - lastBegin.pos);

		ret = lastBegin.valve - sin(radian) *(lastBegin.valve - lastEnd.valve);
	}else {
		float radian = M_PI_2 * (lastBegin.pos - pos) / (lastBegin.pos - lastEnd.pos);
		ret = lastBegin.valve + sin(radian) *(lastEnd.valve - lastBegin.valve);

	}
	return ret;

}

//判断是否在相同的区间
bool AngleAlgorithm::isSameInterval(float lastPos, float newPos) {
	return (int)lastPos == (int)newPos;

}


//计算出新的曲线
void AngleAlgorithm::getInterval(POS_VALVE lastBegin, POS_VALVE lastEnd,
	POS_VALVE lastPos, float pos,
	POS_VALVE* outBegin, POS_VALVE* outEnd) {
	if (isSameInterval(lastPos.pos,pos)){
        //相同区间
		if (isSameLine(lastBegin.pos,lastEnd.pos,lastPos.pos,pos)){
            //相同曲线，不用重新计算
			*outBegin = lastBegin;
			*outEnd = lastEnd;
		}else {
            //不同曲线，需要重新计算
			if (lastEnd.pos > lastBegin.pos) //right
			{
				*outBegin = lastPos;
				outEnd->pos = (int)lastPos.pos;
				outEnd->valve = 1;
			}else{
				*outBegin = lastPos;
				outEnd->pos = (int)lastPos.pos + 1;
				outEnd->valve = 0;
			}
		}
	}else {
        //不同区间，肯定是新的曲线
		bool isRight = true;
		if ( pos > lastPos.pos){
			if (pos - lastPos.pos > _size / 2)
			{
				isRight = false;
			}
		}else{
			if (lastPos.pos - pos <= _size / 2)
			{
				isRight = false;
			}
		}
		if (isRight){
			outBegin->pos = (int)pos;
			outBegin->valve = 1;
			outEnd->pos = (int)pos + 1;
			outEnd->valve = 0;
		}else{
			outBegin->pos = (int)pos + 1;
			outBegin->valve = 0;
			outEnd->pos = (int)pos;
			outEnd->valve = 1;
		}
	}

}
